drop processingdate		// variable for version control only
drop clientid			// single-valued, pretty sure it means us
drop therapeuticclass	// in another dataset

* destring IDs. For some, attach names as values. Otherwise, drop names
destring medid, replace
destring formularyid, replace

foreach var in universalstatus universalstatusrollup rawstatus {
	replace `var' = trim(upper(`var'))
	encode `var', gen(temp) label(`var'lbl)
	drop `var'
	rename temp `var'
	}
	
* format period as a monthly variable
gen m = ym(real(substr(period,1,4)), real(substr(period,-2,2)))
format m %tm
drop period

* now impute missing months within the same year
gen year = yofd(dofm(m))
gen month = month(dofm(m))

egen double xID = group(medid formularyid year)
xtset xID month

tsfill, full

* fill in
foreach var in medid formularyid year {
	bysort xID (`var') : replace `var' = `var'[1] if `var' == .
	}

replace m = mofd(dofy(year)) - 1 + month if m == .

drop xID month
compress

* for each medid, drop leading and trailing observations (i.e., before launch 
* and after it has been withdrawn)
bysort medid : egen last = max(m * (rawstatus != .))
bysort medid : egen first = min(m * (rawstatus != .) + 9999999 * (rawstatus == .))

drop if m > last | m < first
drop first last

compress

* fill in
gen flag_impute = rawstatus == .

foreach var in universalstatus universalstatusrollup rawstatus {
	bysort formularyid medid year (m) : replace `var' = `var'[_n-1] if `var' == . & `var'[_n-1] != .
	}
	
gen negm = -m

foreach var in universalstatus universalstatusrollup rawstatus {
	bysort formularyid medid year (negm) : replace `var' = `var'[_n-1] if `var' == . & `var'[_n-1] != .
	}

drop negm
